package com.zx.znav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.znav.bean.LinkStar;
import com.zx.znav.service.LinkService;
import com.zx.znav.service.LinkStarService;
import com.zx.znav.mapper.LinkStarMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【t_link_star】的数据库操作Service实现
* @createDate 2023-08-14 10:20:31
*/
@Service
public class LinkStarServiceImpl extends ServiceImpl<LinkStarMapper, LinkStar>
    implements LinkStarService{

    @Override
    public void star(Integer linkId, Integer userId) {
        LinkStar linkStar = new LinkStar();
        linkStar.setLinkId(linkId);
        linkStar.setUserId(userId);
        save(linkStar);
    }

    @Override
    public void unstar(Integer linkId, Integer userId) {
        LambdaUpdateWrapper<LinkStar> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LinkStar::getLinkId, linkId);
        wrapper.eq(LinkStar::getUserId, userId);
        remove(wrapper);
    }

    @Override
    public Boolean isStar(Integer linkId, Integer userId) {
        LambdaUpdateWrapper<LinkStar> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LinkStar::getLinkId, linkId);
        wrapper.eq(LinkStar::getUserId, userId);
        return count(wrapper) > 0;
    }

    @Override
    public List<Integer> listStarLink(Integer userId) {
        LambdaQueryWrapper<LinkStar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LinkStar::getUserId, userId);
        wrapper.select(LinkStar::getLinkId);
        return list(wrapper).stream().map(LinkStar::getLinkId).collect(Collectors.toList());
    }
}




